<?php
namespace Modules\Backstage\Controllers;

class IndexController extends \Phalcon\Mvc\Controller {
	public function beforeExecuteRoute(\Phalcon\Mvc\Dispatcher $dispatcher) {
		$this -> response -> setStatusCode(200, 'OK') -> sendHeaders() -> setContentType('application/json', 'UTF-8');

		$service = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($service['code'] != 0) {
			$this -> response -> setJsonContent(['code'=>-1,'msg'=>$service['message'] ]) -> send();
			exit;
		}

		$this -> account::getAclInstance() -> check($this, $dispatcher, $service['model'] -> roles_id);
	}

	public function indexAction() {
		$buyStatus0 = $this -> trade::getBuyServiceInstance() -> sum([
			'conditions' => 'status = 1',
			'column' => 'amount'
		]);
		$sellStatus0 = $this -> trade::getSellServiceInstance() -> sum([
			'conditions' => 'status = 1',
			'column' => 'amount'
		]);
		$buyStatus1 = $this -> trade::getBuyServiceInstance() -> sum([
			'conditions' => 'status = 0',
			'column' => 'amount'
		]);
		$sellStatus1 = $this -> trade::getSellServiceInstance() -> sum([
			'conditions' => 'status = 0',
			'column' => 'amount'
		]);
		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'status_first' => $buyStatus0 + $sellStatus0, 'status_second' => $buyStatus1 + $sellStatus1]);
	}
}